package com.example.petshopping.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.petshopping.model.AppMemberUserInfoRespVO;
import com.google.gson.Gson;

/**
 * 用户信息管理工具类
 */
public class UserInfoManager {
    private static final String PREF_NAME = "pet_shopping_prefs";
    private static final String KEY_USER_INFO = "user_info";
    private static final String KEY_USER_NICKNAME = "user_nickname";
    private static final String KEY_USER_AVATAR = "user_avatar";
    private static final String KEY_USER_MOBILE = "user_mobile";
    private static final String KEY_USER_SEX = "user_sex";
    private static final String KEY_USER_BIRTHDAY = "user_birthday";
    private static final String KEY_USER_POINT = "user_point";
    private static final String KEY_USER_EXPERIENCE = "user_experience";
    private static final String KEY_USER_LEVEL_ID = "user_level_id";
    private static final String KEY_USER_LEVEL_NAME = "user_level_name";
    private static final String KEY_USER_LEVEL_ICON = "user_level_icon";
    private static final String KEY_USER_LEVEL_VALUE = "user_level_value";
    private static final String KEY_BROKERAGE_ENABLED = "brokerage_enabled";

    private static Gson gson = new Gson();

    /**
     * 保存用户信息
     */
    public static void saveUserInfo(Context context, AppMemberUserInfoRespVO userInfo) {
        SharedPreferences sp = getSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        
        // 保存完整的用户信息JSON
        String userInfoJson = gson.toJson(userInfo);
        editor.putString(KEY_USER_INFO, userInfoJson);
        
        // 为了方便单独获取，也分别保存各个字段
        editor.putString(KEY_USER_NICKNAME, userInfo.getNickname());
        editor.putString(KEY_USER_AVATAR, userInfo.getAvatar());
        editor.putString(KEY_USER_MOBILE, userInfo.getMobile());
        if (userInfo.getSex() != null) {
            editor.putInt(KEY_USER_SEX, userInfo.getSex());
        }
        editor.putString(KEY_USER_BIRTHDAY, userInfo.getBirthday());
        if (userInfo.getPoint() != null) {
            editor.putInt(KEY_USER_POINT, userInfo.getPoint());
        }
        if (userInfo.getExperience() != null) {
            editor.putInt(KEY_USER_EXPERIENCE, userInfo.getExperience());
        }
        if (userInfo.getBrokerageEnabled() != null) {
            editor.putBoolean(KEY_BROKERAGE_ENABLED, userInfo.getBrokerageEnabled());
        }
        
        // 保存等级信息
        if (userInfo.getLevel() != null) {
            AppMemberUserInfoRespVO.Level level = userInfo.getLevel();
            if (level.getId() != null) {
                editor.putLong(KEY_USER_LEVEL_ID, level.getId());
            }
            editor.putString(KEY_USER_LEVEL_NAME, level.getName());
            editor.putString(KEY_USER_LEVEL_ICON, level.getIcon());
            if (level.getLevel() != null) {
                editor.putInt(KEY_USER_LEVEL_VALUE, level.getLevel());
            }
        }
        
        editor.apply();
    }

    /**
     * 获取完整用户信息
     */
    public static AppMemberUserInfoRespVO getUserInfo(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        String userInfoJson = sp.getString(KEY_USER_INFO, null);
        
        if (userInfoJson != null && !userInfoJson.isEmpty()) {
            try {
                return gson.fromJson(userInfoJson, AppMemberUserInfoRespVO.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 获取用户昵称
     */
    public static String getUserNickname(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(KEY_USER_NICKNAME, "用户");
    }

    /**
     * 获取用户头像
     */
    public static String getUserAvatar(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(KEY_USER_AVATAR, null);
    }

    /**
     * 获取用户手机号
     */
    public static String getUserMobile(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(KEY_USER_MOBILE, null);
    }

    /**
     * 获取用户性别
     */
    public static Integer getUserSex(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        if (!sp.contains(KEY_USER_SEX)) {
            return null;
        }
        return sp.getInt(KEY_USER_SEX, 0);
    }

    /**
     * 获取用户生日
     */
    public static String getUserBirthday(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(KEY_USER_BIRTHDAY, null);
    }

    /**
     * 获取用户积分
     */
    public static Integer getUserPoint(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        if (!sp.contains(KEY_USER_POINT)) {
            return null;
        }
        return sp.getInt(KEY_USER_POINT, 0);
    }

    /**
     * 获取用户经验值
     */
    public static Integer getUserExperience(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        if (!sp.contains(KEY_USER_EXPERIENCE)) {
            return null;
        }
        return sp.getInt(KEY_USER_EXPERIENCE, 0);
    }

    /**
     * 获取用户等级名称
     */
    public static String getUserLevelName(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(KEY_USER_LEVEL_NAME, "普通会员");
    }

    /**
     * 获取用户等级ID
     */
    public static Long getUserLevelId(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        if (!sp.contains(KEY_USER_LEVEL_ID)) {
            return null;
        }
        return sp.getLong(KEY_USER_LEVEL_ID, 0);
    }

    /**
     * 获取用户等级图标
     */
    public static String getUserLevelIcon(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.getString(KEY_USER_LEVEL_ICON, null);
    }

    /**
     * 获取用户等级数值
     */
    public static Integer getUserLevelValue(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        if (!sp.contains(KEY_USER_LEVEL_VALUE)) {
            return null;
        }
        return sp.getInt(KEY_USER_LEVEL_VALUE, 0);
    }

    /**
     * 获取是否启用推广员功能
     */
    public static Boolean getBrokerageEnabled(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        if (!sp.contains(KEY_BROKERAGE_ENABLED)) {
            return null;
        }
        return sp.getBoolean(KEY_BROKERAGE_ENABLED, false);
    }

    /**
     * 清除用户信息
     */
    public static void clearUserInfo(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        SharedPreferences.Editor editor = sp.edit();
        
        editor.remove(KEY_USER_INFO);
        editor.remove(KEY_USER_NICKNAME);
        editor.remove(KEY_USER_AVATAR);
        editor.remove(KEY_USER_MOBILE);
        editor.remove(KEY_USER_SEX);
        editor.remove(KEY_USER_BIRTHDAY);
        editor.remove(KEY_USER_POINT);
        editor.remove(KEY_USER_EXPERIENCE);
        editor.remove(KEY_USER_LEVEL_ID);
        editor.remove(KEY_USER_LEVEL_NAME);
        editor.remove(KEY_USER_LEVEL_ICON);
        editor.remove(KEY_USER_LEVEL_VALUE);
        editor.remove(KEY_BROKERAGE_ENABLED);
        
        editor.apply();
    }

    /**
     * 检查是否有用户信息
     */
    public static boolean hasUserInfo(Context context) {
        SharedPreferences sp = getSharedPreferences(context);
        return sp.contains(KEY_USER_INFO);
    }

    /**
     * 获取SharedPreferences实例
     */
    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    }
} 